﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BitLaboratory.DbAccess
{
    public class DataAccessFactory : IDataAccessFactory
    {
        private IDatabaseProvider _databaseProvider;
        private string _connectionString;

        public DataAccessFactory(string connectionString)
            : this(new MsSqlDatabaseProvider(connectionString))
        {
            this._connectionString = connectionString;
        }

        public DataAccessFactory(IDatabaseProvider provider)
        {
            this._databaseProvider = provider;
            this._connectionString = this._databaseProvider.ConnectionString;
        }

        private DataAccessFactory(IDatabaseProvider provider, string connectionString)
        {
            this._databaseProvider = provider;
            this._connectionString = connectionString;
        }

        protected virtual IDataAccessFactory Clone()
        {
            return new DataAccessFactory(this._databaseProvider, this._connectionString);
        }

        public IDataAccess Create()
        {
            return new DataAccess(this);
        }

        public IDataAccess Create(string connectionString)
        {
            IDataAccessFactory factory = this.Clone();
            factory.ConnectionString = connectionString;
            return new DataAccess(factory);
        }

        public IDatabaseProvider DatabaseProvider
        {
            get { return _databaseProvider; }
        }
        
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }

        object ICloneable.Clone()
        {
            return this.Clone();
        }
    }
}
